--  Цвета:
--            R         G         B
-- Белый  1111_1111 1111_1111 1111_1111
-- Серый  1100_0000 1100_0000 1100_0000
-- Черный 0000_0000 0000_0000 0000_0000
--
LIBRARY ieee;
USE ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;

ENTITY CompImage IS
GENERIC(
 pixels_y :	INTEGER := 320;    --row that first color will persist until
 pixels_x	:	INTEGER := 240);   --column that first color will persist until
PORT(
 disp_ena  : IN  STD_LOGIC;	--display enable ('1' = display time, '0' = blanking time)
 RamData1  : IN  std_logic_vector(7 DOWNTO 0);
 RamData2  : IN  std_logic_vector(7 DOWNTO 0);
 Frame1    : IN  STD_LOGIC;
 Frame2    : IN  STD_LOGIC; 
 row		  : IN  std_logic_vector(31 downto 0);		--row pixel coordinate
 column	  : IN  std_logic_vector(31 downto 0);		--column pixel coordinate
 red		  : OUT STD_LOGIC_VECTOR(7 DOWNTO 0) := (OTHERS => '0');  --red magnitude output to DAC
 green	  : OUT STD_LOGIC_VECTOR(7 DOWNTO 0) := (OTHERS => '0');  --green magnitude output to DAC
 blue		  : OUT STD_LOGIC_VECTOR(7 DOWNTO 0) := (OTHERS => '0'); --blue magnitude output to DAC
 RamRdEn   : OUT std_logic
); 
END CompImage;

ARCHITECTURE ArchImage OF CompImage IS
signal sigRdEn : std_logic := '0';

BEGIN
	PROCESS(disp_ena)
    VARIABLE sigRow : integer := 0;
    VARIABLE sigColumn : integer := 0;
	BEGIN
  	 IF(disp_ena = '1') THEN		--display time
     sigRow := conv_integer(row);
     sigColumn := conv_integer(column);
     IF(
	    (sigRow < 640+pixels_y/2) AND (sigColumn <= 512+pixels_x/2)
			AND
		  (sigRow > 640-pixels_y/2) AND (sigColumn >= 512-pixels_x/2)
		 )THEN
		  --red <= "11110000";--(OTHERS => '1');
		  --green	<= "11110000";--(OTHERS => '1');
		  --blue <= "11110000";--(OTHERS => '1');
		  IF (Frame1 = '1') THEN
		   red <= RamData1;--(OTHERS => '1');
		   green	<= RamData1;--(OTHERS => '1');
		   blue <= RamData1;--(OTHERS => '1');
		  END IF;
		  IF (Frame2 = '1') THEN
		   red <= RamData2;--(OTHERS => '1');
		   green	<= RamData2;--(OTHERS => '1');
		   blue <= RamData2;--(OTHERS => '1');
		  END IF;
		  sigRdEn <= '1';
		 ELSE
		  red <= (OTHERS => '1');
		  green	<= (OTHERS => '1');
		  blue <= (OTHERS => '0');
		  sigRdEn <= '0';
		 END IF;
		ELSE								--blanking time
			red <= (OTHERS => '0');
			green <= (OTHERS => '0');
			blue <= (OTHERS => '0');
			sigRdEn <= '0';
	 END IF;
	END PROCESS;
RamRdEn <= sigRdEn;

END ArchImage;